<?php
	include "common.php";
	session_start();
	error_reporting(0);
	if($_SESSION['logInCheck']!=1){
		header('Location: index.php?loginInfo=false');
	}
	$systemDate = date('y-m-d');
	$approval = $_GET['id'];
	$employeeId = $_SESSION['id'];
	$userFname = $_SESSION['userFname'];
	$userLname = $_SESSION['userLname'];
	$supervisor = $_SESSION['supervisor'];

	$sql ="SELECT * FROM notification WHERE supervisor = '$employeeId'";
	$sqlResult = mysql_query($sql);
	$employeeIdResult = mysql_fetch_assoc ($sqlResult);
	$applicant = $_SESSION['applier'];
	
	//******************* Update Notification Table ********************
	if ($approval == 1)
	{
		$applyType = $_SESSION['apply_type'];
		
		mysql_query("UPDATE notification SET Status ='ACCEPTED' WHERE e_id = '$applicant' and supervisor = '$employeeId'");
		mysql_query("UPDATE apply_leave SET approval = 'ACCEPTED' WHERE e_id = '$applicant' and supervisor = '$employeeId'");
		if ($applyType == 'apply_leave')
		{
			$applyLeaveSql = "SELECT * FROM $applyType WHERE e_id = '$applicant'";
			$applyLeaveSqlResult = mysql_query($applyLeaveSql);
			$applyLeaveFetch  = mysql_fetch_assoc($applyLeaveSqlResult);
			
			$applyLeave = $applyLeaveFetch['leave_type'];
			$leaveFrom = $applyLeaveFetch['leave_from'];
			$leaveTo = $applyLeaveFetch['leave_to'];
			
			$leaveDuration = dateDiff($leaveFrom, $leaveTo);
			$leaveDuration += 1;
			
			$separator = explode("-",$leaveFrom);
			$leaveMonth = $separator[1];
							
			$casualSql = "SELECT * FROM earn_casual_leave WHERE e_id = '$applicant'";
			$casualSqlQuery = mysql_query($casualSql);
			$casualSqlQueryResult = mysql_fetch_assoc($casualSqlQuery);
			
			if ($leaveMonth == 1)
			{
				$sql = "UPDATE earn_casual_leave SET Jan = Jan+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 2)
			{
				$sql = "UPDATE earn_casual_leave SET Feb = Feb+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 3)
			{
				$sql = "UPDATE earn_casual_leave SET Mar = Mar+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 4)
			{
				$sql = "UPDATE earn_casual_leave SET Apr = Apr+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 5)
			{
				$sql = "UPDATE earn_casual_leave SET May = May+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 6)
			{
				$sql = "UPDATE earn_casual_leave SET Jun = Jun+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 7)
			{
				$sql = "UPDATE earn_casual_leave SET Jul = Jul+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 8)
			{
				$sql = "UPDATE earn_casual_leave SET Aug = Aug+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 9)
			{
				$sql = "UPDATE earn_casual_leave SET Sep = Sep+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 10)
			{
				$sql = "UPDATE earn_casual_leave SET Oct = Oct+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else if ($leaveMonth == 11)
			{
				$sql = "UPDATE earn_casual_leave SET Nov = Nov+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			else 
			{
				$sql = "UPDATE earn_casual_leave SET Dec = Dec+$leaveDuration WHERE e_id = '$applicant'";
				$sqlQuery = mysql_query($sql);
			}
			
		
			if ($applyLeave == 'Earn')
			{			
				//*************** Update earn_casual_leave table *******************
				echo $earnLeaveSql = "UPDATE earn_casual_leave SET enjoyed= enjoyed + $leaveDuration, available_leave = available_leave - $leaveDuration  WHERE e_id = '$applicant'";
				$earnLeaveSqlResult = mysql_query($earnLeaveSql);		
			}
			else if ($applyLeave == 'Casual')
			{
				$casualLeaveSql = "UPDATE earn_casual_leave SET c_enjoyed = c_enjoyed+1,c_avail = c_avail-1 WHERE e_id = '$applicant'";
				$casualLeaveSqlResult = mysql_query($casualLeaveSql);
			}
		}
		else if ($applyType == 'apply_for_work_tonight')
		{
			$workNightSql = "UPDATE apply_for_work_tonight SET status = 'ACCEPTED' WHERE e_id = '$applicant'";
			$workNightSqlQuery = mysql_query($workNightSql);
			
			$workSql = "SELECT * FROM apply_for_work_tonight WHERE e_id = '$applicant' AND status = 'ACCEPTED'";
			$workSqlQuery = mysql_query($workSql);
			$workSqlResult = mysql_fetch_assoc($workSqlQuery);
			
			$date1 = $workSqlResult['work_from'];
			$date2 = $workSqlResult['work_to'];
			
			//$newDate1 = strtotime($date1);
			//$newDate2 = strtotime($date2);
			//$todaysDate = strtotime($systemDate);
			
			if ($todaysDate >= $newDate1)
			{
				if ( $todaysDate <=  $newDate2)
				{
					$dateDifference = dateDiff($date1,$systemDate);
					for ($i=0;$i<$dateDifference;$i++)
					{
						echo $updateSql = "UPDATE attendance SET status = 1 WHERE e_id = '$applicant' AND date='$date1'";
						$updateSqlQuery = mysql_query($updateSql);
						
						$date1 = strtotime(date("y-m-d", strtotime($date1))." +1 day");
						$date1 = date('y-m-d', $date1);
					}
				}
			}
			else if ($todaysDate >= $newDate2)
			{
				$dateDifference = dateDiff($date1,$date2);
				for ($i=0;$i<$dateDifference;$i++)
				{
					echo $updateSql = "UPDATE attendance SET status = 1 WHERE e_id = '$applicant' AND date='$date1'";
					$updateSqlQuery = mysql_query($updateSql);
					
					$date1 = strtotime(date("y-m-d", strtotime($date1))." +1 day");
					$date1 = date('y-m-d', $date1);
				}
			}
		}
		else if ($applyType == 'medical_doc')
		{
			$updateSql = "UPDATE attendance SET status = '1' WHERE e_id = '$applicant'";
			$updateSqlQuery = mysql_query($updateSql);
			
			$notificationSql = "UPDATE notification SET Status = 'ACCEPTED' WHERE e_id = '$applicant'";
			$notificationSqlQuery = mysql_query($notificationSql);
		}
		else if ($applyType == 'apply_for_meeting')
		{
			$updateSql = "UPDATE notification SET Status = 'ACCEPTED' WHERE e_id = '$applicant'";
			$updateSqlQuery = mysql_query($updateSql);
			
			$meetingSql = "UPDATE apply_for_meeting SET status = 'ACCEPTED' WHERE e_id = '$applicant'";
			$meetingSqlQuery = mysql_query($meetingSql);
			
			$sql = "SELECT * FROM apply_for_meeting WHERE e_id = '$applicant' AND status = 'ACCEPTED'";
			$sqlQuery = mysql_query($sql);
			$sqlQueryResult = mysql_fetch_assoc($sqlQuery);
			
			$dateFrom = $sqlQueryResult['date_from'];
			$dateTo = $sqlQueryResult['date_to'];
			
			$dateDifference = dateDiff($dateFrom,$dateTo);
			
			$deleteSql = "DELETE FROM attendance WHERE e_id = '$applicant' AND date between '$dateFrom' AND '$dateTo'";
			$deleteSqlQuery = mysql_query($deleteSql);
			
			for ($i=0;$i<=$dateDifference;$i++)
			{
				$insertSql = "INSERT INTO attendance (e_id,date,status,check_logged_in,inTheOffice) VALUES('$applicant','$dateFrom','1','1','true')";
				$insertSqlQuery = mysql_query($insertSql);
				
				$dateFrom = strtotime(date("y-m-d", strtotime($dateFrom))." +1 day");
				$dateFrom = date('y-m-d', $dateFrom);
			}
		}
		else if ($applyType == 'reason_for_late')
		{
			echo $updateSql = "UPDATE notification SET Status = 'ACCEPTED' WHERE e_id = '$applicant'";
			$updateSqlQuery = mysql_query($updateSql);
			
			echo $reasonSql = "UPDATE reason_for_late SET status = 'ACCEPTED' WHERE e_id = '$applicant'";
			$reasonSqlQuery = mysql_query($reasonSql); 
			
			$userSql = "SELECT * FROM user WHERE e_id = '$applicant'";
			$userSqlQuery = mysql_query($userSql);
			$userSqlQueryResult = mysql_fetch_assoc($userSqlQuery);
			
			$totalLate = $userSqlQueryResult['t_late'];
			$totalLate--;
			
			echo "Done !";
			
			$lateSql = "UPDATE user SET t_late = '$totalLate' WHERE e_id = '$applicant'";
			$lateSqlQuery = mysql_query($lateSql);
		}
	
	}
	else
	{
		mysql_query("UPDATE notification SET Status ='REJECTED' WHERE e_id = '$applicant' and supervisor = '$employeeId'");
	}
	$applyType = $_SESSION['apply_type'];
	
	

		function dateDiff($start, $end) {
		
		$start_ts = strtotime($start);
		
		$end_ts = strtotime($end);
		
		$diff = $end_ts - $start_ts;
		
		return round($diff / 86400);
		
		}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="index.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="show_report.css" />
<link rel="icon" type="image/ico" href="images/favicon.ico"></link> 
<link rel="shortcut icon" href="images/favicon.ico"></link>
<script src="curvycorners.src.js" type="text/javascript"></script>
<script src="login.js" type="text/javascript"></script>
<title><?php echo "Welcome to ".$userFname." ".$userLname; ?></title>
</head>

<body>
<div class="main_div_other">
	<p><img src="images/creato_logo.jpg" /></p>
    <p id="creatobd">www.creatobd.com</p>
    <p id="date"><script>document.write(new_month+" "+new_date+", "+old_date.getFullYear()+" || "+new_day);</script></p>
    <div class="other_page_icons">
    	<div class="new_notification">
        	<img src="images/arrow_up.JPG" /><?php if ($notification >0){?><p>You have<?php echo " ".$notification." ";?>new notification</p><?php }?>
        </div><!--end of new_notification-->
    	<ul>
        	<a href="ceo.php"><li><div id="home"></div><p>home</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
            <a href="ceo_employee.php"><li><div id="employee"></div><p>employee</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
            <a href="on_duty.php"><li><div id="onduty"></div><p>on duty</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
            <a href="ceo_attendance.php"><li><div id="attendance"></div><p>attendance</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
            <a href="leave.php"><li><div id="leave"></div><p>leave</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
            <a href="ceo_notification.php"><li><div id="notification"></div><p>notification</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
            <a href="ceo_blackbox.php"><li><div id="blackbox"></div><p>black box</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
            <a href="#"><li><div id="hrpolicy"></div><p>hr policy</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
            <a href="index.php"><li><div id="logout"></div><p>log out</p><p class="present_class"><img src="images/arrow_up.JPG" /></p></li></a>
        </ul>
    </div><!-- end of other_page_icons-->
    
</div><!-- end of main_div_other-->
<div class="othrpage_1">
    <p>Just Show The Notification Table</p>
</div><!-- end of othrpage_1-->
<div class="human_portal">
  		<p>creato human resourse portal</p>
</div><!-- end of human_portal-->
<div class="page_content">	
	<p>Thank You Very Much...</p>
	<p>The Employee Will Be Notified Shortly...</p>
</div>
</body>
</html>